package com.ruoyi.tzzl.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.tzzl.domain.WzFocuson;
import com.ruoyi.tzzl.domain.WzMessageLogging;
import com.ruoyi.tzzl.domain.WzProgramUser;
import com.ruoyi.tzzl.domain.bo.WzFocusonBo;
import com.ruoyi.tzzl.domain.vo.QueryVo;
import com.ruoyi.tzzl.mapper.WzFocusonMapper;
import com.ruoyi.tzzl.service.IWzFocusonService;
import com.ruoyi.tzzl.service.IWzMessageLoggingService;
import com.ruoyi.tzzl.service.IWzProgramUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 用户关注Service业务层处理
 * @author by caoyong
 * @date 2022/11/18
 **/
@Service
public class WzFocusonServiceImpl extends ServiceImpl<WzFocusonMapper, WzFocuson> implements IWzFocusonService {


    @Autowired
    private WzFocusonMapper wzFocusonMapper;

    @Autowired
    private IWzProgramUserService userService;

    @Autowired
    private IWzMessageLoggingService wzMessageLoggingService;

    @Override
    public boolean follow(WzFocuson wzFocuson) {
      LambdaQueryWrapper<WzFocuson> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzFocuson::getWid,wzFocuson.getWid());
        lqw.eq(WzFocuson::getFid,wzFocuson.getFid());
        WzFocuson wzFocuson1 = this.getOne(lqw);
        if (ObjectUtils.isEmpty(wzFocuson1)){
            wzFocuson.setCreateTime(new Date());
            //添加消息记录
            WzMessageLogging wzMessageLogging =new WzMessageLogging();
            wzMessageLogging.setCreateTime(new Date()); //创建时间
            //产生消息用户消息
           WzProgramUser wzProgramUser = userService.getById(wzFocuson.getWid());
            wzMessageLogging.setProduceAvatarurl(wzProgramUser.getAvatarurl()); //产生消息的用户头像
            wzMessageLogging.setProduceId(wzProgramUser.getId()); //产生消息的用户id
            wzMessageLogging.setProduceName(wzProgramUser.getUserName()); //产生消息的用户名称
            //接收消息用户消息
            WzProgramUser user =userService.getById(wzFocuson.getFid());
            wzMessageLogging.setProduceType("2"); //类型
            wzMessageLogging.setReceiceAvatarurl(user.getAvatarurl()); //接收消息的用户头像
            wzMessageLogging.setReceiveId(user.getId()); //接收消息的用户id
            wzMessageLogging.setReceiveName(user.getUserName()); //接收消息的用户名称
            wzMessageLoggingService.save(wzMessageLogging);
           return this.save(wzFocuson);
        }

        return false;
    }

    /**
     * 取消关注
     *
     * @param wzFocuson
     */
    @Override
    public boolean attention(WzFocuson wzFocuson) {
        LambdaQueryWrapper<WzFocuson> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzFocuson::getWid,wzFocuson.getWid());
        lqw.eq(WzFocuson::getFid,wzFocuson.getFid());
        WzFocuson wzFocuson1 = this.getOne(lqw);
        return this.removeById(wzFocuson1.getId());
    }

    /**
     * 查询用户关注列表
     *
     * @param wzFocusonBo
     */
    @Override
    public List<WzFocusonBo> attentionList(QueryVo queryVo) {
        return wzFocusonMapper.attentionList(queryVo);
    }

    /**
     * 查询是否已经关注用户
     * return 0-未关注  1-已关注
     *
     * @param id
     * @param userId
     */
    @Override
    public String judgeAttention(Long Wid, Long userId) {
        LambdaQueryWrapper<WzFocuson> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzFocuson::getWid,Wid);
        lqw.eq(WzFocuson::getFid,userId);
        WzFocuson wzFocuson = this.getOne(lqw);
        if (ObjectUtils.isNotEmpty(wzFocuson)){
            return "1";
        }else{
            return "0";
        }
    }

    /**
     * 查看谁关注了我
     *
     * @param id
     */
    @Override
    public List<WzFocuson> beAttention(Long id) {
        LambdaQueryWrapper<WzFocuson> lqw =new LambdaQueryWrapper<>();
        lqw.eq(WzFocuson::getFid,id);
       List<WzFocuson> list = this.list(lqw);
       if (ObjectUtils.isNotEmpty(list)){
           for (int i = 0; i < list.size(); i++) {
               list.get(i).setUser(userService.getById(list.get(i).getWid()));
           }
       }
        return list;
    }

}
